/*** Class Name: ifulfillNotesRestAPIBatch 
* Author: Accenture      
* Requirement/Project Name: iFULFILL
* Requirement/Project Description:SFDC should verify that the CSS system is not down prior to sending an Notes outbound message.
* This batch will process all the Failed Notes records where status code not equal to '0 .
* Failed records are send to SIL and update the records based upon the response.
* **/
global class ifulfillNotesRestAPIBatch implements Database.Batchable<sObject>,Database.AllowsCallouts,Database.Stateful{

    public String query;
    public static List<sObject> objectList;
    static AppEndPoints__c appEndPoints;
    
    global database.querylocator start(Database.BatchableContext BC){
       return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<sObject> scope){    
        objectList = new List<sObject>();
        objectList =scope;   
        connectToSil();
    }  
    
    static void connectToSil(){      
      String endPointURL;
      String bodyData;
      String lastModifiedDate;
      String []responseArray;
      String notesData='';
      appEndPoints = AppEndPoints__c.getInstance();
      List<Notes__c> restAPIReponseNotesList= new List<Notes__c>();      
      try{ 
          for(Sobject obj:objectList){
            Notes__c notes= (Notes__c)obj;
            lastModifiedDate= notes.LastModifiedDate.format('MM/dd/yyyy');
            if(notes.Notes__c!=null)
                notesData=notes.Notes__c+'';  
                   
            endPointURL=appEndPoints.workflowCompleteURL__c+'/UpdateIRFulfillmentNote?CreatedById='+notes.CreatedByID+'&FullfilmentNotes='+notes.Fullfilment_Notes__c+'&Id='+notes.ID+'&LastModifiedDate='+lastModifiedDate+'&Notes='+EncodingUtil.urlEncode(notesData,'UTF-8');                  
            bodyData='CreatedById='+notes.CreatedByID+'&FullfilmentNotes='+notes.Fullfilment_Notes__c+'&Id='+notes.ID+'&LastModifiedDate='+lastModifiedDate+'&Notes='+EncodingUtil.urlEncode(notesData,'UTF-8');
            responseArray =ifulfillWebService.invokeRestAPICall(endPointURL,'Notes',bodyData); 
                
            if(responseArray[0] !=null && responseArray[0].equals('0')){ 
               notes.Delivery_Status__c=IfullfillTextConstant.FI_PR_DELIVERY_SUCCESS_STATUS;
               notes.Status_Code__c=responseArray[0];
               restAPIReponseNotesList.add(notes);                
            }else{
                notes.Delivery_Status__c=IfullfillTextConstant.FI_PR_DELIVERY_FAILED_STATUS;
                notes.Status_Code__c=responseArray[0];
                notes.Error_Message__c=responseArray[1];
                notes.Error_Stack__c=responseArray[2];
                restAPIReponseNotesList.add(notes);                    
           }      
        } 
        System.debug('restAPIReponseNotesList--->'+restAPIReponseNotesList);
        if(restAPIReponseNotesList.size()>0){
            update restAPIReponseNotesList;
        }

      }Catch(Exception e){
        System.debug('Exception is '+e);          
      }
    } 
     global void finish(Database.BatchableContext BC){
       
    }

 

}